using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using OhioState.AssetManaged;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;
using OhioState.Math;


namespace ShapeEditor
{
    
    public partial class ShapeControl : UserControl
    {
        public event IAssetManagedHandler ChangedEvent;
        protected EventsStrategyAbstract _eventsHelper;

        private Plane plane;
        private String orient;
        private ROIBoxProxy roi;
        private List<ClipPlaneProxy> clips;
        private ClipPlaneProxy clip1, clip2, clip3;

        public ShapeControl()
        {
            InitializeComponent();
            _eventsHelper = new EventsStrategyAsync();

            //
            // Set up event handler for selection changed event
            //
            assetManagerControl1.SelectedEvent += new IAssetManagedListHandler(assetManagerControl1_SelectedEvent);
            assetManagerControl1.UpdatedEvent += new IAssetManagedHandler(assetManagerControl1_UpdatedEvent);
            assetManagerControl2.SelectedEvent += new IAssetManagedListHandler(assetManagerControl2_SelectedEvent);
            assetManagerControl2.UpdatedEvent += new IAssetManagedHandler(assetManagerControl2_UpdatedEvent);
            assetManagerControl3.SelectedEvent += new IAssetManagedListHandler(assetManagerControl3_SelectedEvent);
            assetManagerControl3.UpdatedEvent += new IAssetManagedHandler(assetManagerControl3_UpdatedEvent);
            
            // Choosing ROI for a clip plane
            //assetManagerDropDownControl1.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl1_SelectedEvent);
           
            //// Choosing clip planes
            //assetManagerDropDownControl2.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl2_SelectedEvent);
            //assetManagerDropDownControl3.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl3_SelectedEvent);
            //assetManagerDropDownControl4.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl4_SelectedEvent);
            
            // Choosing Plane before creating ClipPlaneProxy
            assetManagerDropDownControl5.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl5_SelectedEvent);
           
            //
            // Initialize the control
            //
            
            List<Type> types1 = new List<Type>();
            types1.Add(typeof(IShape));
            types1.Add(typeof(IPlane));
            assetManagerControl1.Init(types1);

            List<Type> types2 = new List<Type>();
            types2.Add(typeof(IDrawable));
            assetManagerControl2.Init(types2);

            List<Type> types3 = new List<Type>();
            types3.Add(typeof(ClipPlaneProxy));
            assetManagerControl3.Init(types3);

            //Add a "NoClip" Dummy object to the control
            ClipPlaneProxy noclip = new ClipPlaneProxy("NoClip");
            AssetManager.Instance.Add((IAssetManaged)noclip);

            //assetManagerDropDownControl1.Init(typeof(IDrawable));
            //assetManagerDropDownControl2.Init(typeof(ClipPlaneProxy));
            //assetManagerDropDownControl3.Init(typeof(ClipPlaneProxy));
            //assetManagerDropDownControl4.Init(typeof(ClipPlaneProxy));
            assetManagerDropDownControl5.Init(typeof(Plane));

            orient = null;
            plane = null;
            roi = null;
            clips = new List<ClipPlaneProxy>();
            clip1 = clip2 = clip3 = noclip;

            //assetManagerDropDownControl2.Add
            //
            // Set up the drop down for the templates
            //
            
            BuiltTemplateDropDownShape();
            BuiltTemplateDropDownDrawable();

            //Set No Clip initially for all the clipplanes
            //assetManagerDropDownControl2.assetManagerDropDown_SelectItemEvent(clip1);
            //assetManagerDropDownControl3.assetManagerDropDown_SelectItemEvent(clip2);
            //assetManagerDropDownControl4.assetManagerDropDown_SelectItemEvent(clip3);
        }

       
        private void assetManagerDropDownControl1_SelectedEvent(IAssetManaged asset)
        {
            //if (asset != null)
            //{
            //    roi = (ROIBoxProxy)asset;
            //    //clip1 = roi.Clip1;
            //    //clip2 = roi.Clip2;
            //    //clip3 = roi.Clip3;

            //    //assetManagerDropDownControl2.SelectItem(clip1 as IAssetManaged);
            //    IAssetManagedHandler newEvent = new IAssetManagedHandler(assetManagerDropDownControl2.assetManagerDropDown_SelectItemEvent);
            //    object[] args = new object[1];
            //    args[0] = (IAssetManaged)clip1;
            //    //
            //    // We need to make sure these methods are executed on the correct thread.
            //    //
            //    try
            //    {
            //        if (this.InvokeRequired)
            //            this.Invoke(newEvent, args);
            //        else
            //            newEvent((IAssetManaged)clip1);
            //    }
            //    catch { }
            //    IAssetManagedHandler newEvent1 = new IAssetManagedHandler(assetManagerDropDownControl3.assetManagerDropDown_SelectItemEvent);
            //    object[] args1 = new object[1];
            //    args1[0] = (IAssetManaged)clip2;
            //    //
            //    // We need to make sure these methods are executed on the correct thread.
            //    //
            //    try
            //    {
            //        if (this.InvokeRequired)
            //            this.Invoke(newEvent1, args1);
            //        else
            //            newEvent1((IAssetManaged)clip2);
            //    }
            //    catch { }
            //    IAssetManagedHandler newEvent2 = new IAssetManagedHandler(assetManagerDropDownControl4.assetManagerDropDown_SelectItemEvent);
            //    object[] args2 = new object[1];
            //    args2[0] = (IAssetManaged)clip3;
            //    //
            //    // We need to make sure these methods are executed on the correct thread.
            //    //
            //    try
            //    {
            //        if (this.InvokeRequired)
            //            this.Invoke(newEvent2, args2);
            //        else
            //            newEvent2((IAssetManaged)clip3);
            //    }
            //    catch { }
            //}
        }
        private void assetManagerDropDownControl2_SelectedEvent(IAssetManaged asset)
        {
            //if (roi != null && asset != null)
            //{
            //    clip1 = (ClipPlaneProxy)asset;
            //    roi.ChangeClip(clip1, 1);
            //}
            
        }

        private void assetManagerDropDownControl3_SelectedEvent(IAssetManaged asset)
        {
            //if (roi != null && asset != null)
            //{
            //    clip2 = (ClipPlaneProxy)asset;
            //    roi.ChangeClip(clip2, 2);
            //}
        }

        private void assetManagerDropDownControl4_SelectedEvent(IAssetManaged asset)
        {
            //if (roi != null && asset != null)
            //{
            //    clip3 = (ClipPlaneProxy)asset;
            //    roi.ChangeClip(clip3, 3);
            //}

        }
        private void assetManagerDropDownControl5_SelectedEvent(IAssetManaged asset)
        {
            plane = (Plane)asset;
            IAssetManaged chk = AssetManager.Instance.Find(typeof(ClipPlaneProxy), "Clip" + orient + plane.Name);
            if (chk != null)
                button1.Enabled = false;
            else
                button1.Enabled = true;
        }

        //
        // Setup the drop down list of all templates
        //  Clear the Drop down menu and then add each item from the template list to the
        //  drop down menu.  Create preview icons as well.
        //
        private void BuiltTemplateDropDownShape()
        {
            newToolStripButton.DropDownItems.Clear();
            Image image = new Bitmap(32, 32); // global::ShapeEditor.Properties.Resources.roi_box;
            newToolStripButton.DropDownItems.Add("Box", image, CreateNewFromTemplate1);
            image = new Bitmap(32, 32); //global::ShapeEditor.Properties.Resources.plane1;
            newToolStripButton.DropDownItems.Add("Plane", image, CreateNewFromTemplate1);
            image = new Bitmap(32, 32); //global::ShapeEditor.Properties.Resources.plane1;
            newToolStripButton.DropDownItems.Add("Sphere", image, CreateNewFromTemplate1);

        }

        private void BuiltTemplateDropDownDrawable()
        {
            newtoolStripDrawableButton.DropDownItems.Clear();
            Image image = new Bitmap(32, 32); //global::ShapeEditor.Properties.Resources.roi_box;
            newtoolStripDrawableButton.DropDownItems.Add("ROI", image, CreateNewFromTemplate2);
            newtoolStripDrawableButton.DropDownItems.Add("Boundary", image, CreateNewFromTemplate2);
            newtoolStripDrawableButton.DropDownItems.Add("Outline", image, CreateNewFromTemplate2);
            newtoolStripDrawableButton.DropDownItems.Add("Sphere", image, CreateNewFromTemplate2);
            image = new Bitmap(32, 32); //global::ShapeEditor.Properties.Resources.plane1;
            newtoolStripDrawableButton.DropDownItems.Add("AxesPlane", image, CreateNewFromTemplate2);
            newtoolStripDrawableButton.DropDownItems.Add("SlicePlane", image, CreateNewFromTemplate2);
            newtoolStripDrawableButton.DropDownItems.Add("ClippedDrawable", image, CreateNewFromTemplate2);

        }

        //
        // Event handler for Shapes Template Drop down clicks
        //
        public void CreateNewFromTemplate1(Object sender, EventArgs e)
        {
            if (sender.GetType() == typeof(ToolStripMenuItem))
            {
                String name = ((ToolStripMenuItem)sender).Text;
                if (name == "Box")
                {
                    planeControl1.Visibility(false);
                    boxControl1.Visibility(true);
                    IShape box = new SharedBox("Box");
                    AssetManager.Instance.Add(box as IShape as IAssetManaged);
                    
                }
                else if (name == "Plane")
                {
                    boxControl1.Visibility(false);
                    planeControl1.Visibility(true);
                    Plane plane = new Plane("Plane");
                    AssetManager.Instance.Add(plane);
                }
                else if (name == "Sphere")
                {
                    boxControl1.Visibility(false);
                    planeControl1.Visibility(false);
                    SharedSphere sp = new SharedSphere("Sphere");
                    AssetManager.Instance.Add(sp);
                }
            }
        }

        //
        // Event handler for Drawables Template Drop down clicks
        //
        public void CreateNewFromTemplate2(Object sender, EventArgs e)
        {
            if (sender.GetType() == typeof(ToolStripMenuItem))
            {
                String name = ((ToolStripMenuItem)sender).Text;
                if (name == "ROI")
                {
                    slicePlaneControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    boundaryControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(true);
                    IShape iShape = regionOfInterestControl1.SelectDefault();
                    if (iShape != null)
                    {
                        ROIBoxProxy roiBoxProxy = new ROIBoxProxy("ROI", iShape as SharedBox);
                        AssetManager.Instance.Add(roiBoxProxy);
                    }
                    else
                    {
                        regionOfInterestControl1.Enabled = false;
                    }
                }
                else if (name == "Sphere")
                {
                    slicePlaneControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);
                    boundaryControl1.Visibility(false);

                    //
                    // HACK  
                    // TODO: Need to add a sphere control to select the shared sphere.
                    //
                    List<IAssetManaged> shapes = AssetManager.Instance.FindAll(typeof(SharedSphere));
                    if (shapes.Count > 0)
                    {
                        SphereProxy sphereProxy = new SphereProxy("Sphere", shapes[0] as SharedSphere);
                        AssetManager.Instance.Add(sphereProxy);
                    }
                }

                else if (name == "Boundary")
                {
                    slicePlaneControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);
                    boundaryControl1.Visibility(true);
                    IShape iShape = boundaryControl1.SelectShapeDefault();
                    if (iShape != null)
                    {
                        BoxBoundaryProxy boxBoundaryProxy = new BoxBoundaryProxy("Boundary", iShape as SharedBox);
                        AssetManager.Instance.Add(boxBoundaryProxy);
                    }
                    else
                    {
                        boundaryControl1.Enabled = false;
                    }
                }

                else if (name == "Outline")
                {
                    slicePlaneControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    boundaryControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);
                    outlineControl1.Visibility(true);
                    IShape iShape = outlineControl1.SelectShapeDefault();
                    if (iShape != null)
                    {
                      BoxOutlineProxy boxoutlineproxy = new BoxOutlineProxy("Outline", iShape as SharedBox);
                      AssetManager.Instance.Add(boxoutlineproxy);
                    }
                    else
                    {
                        outlineControl1.Enabled = false;
                    }

                }
                else if (name == "AxesPlane")
                {
                    slicePlaneControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(false);
                    boundaryControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);
                    axesPlaneControl1.Visibility(true);
                    IShape iShape = axesPlaneControl1.SelectShapeDefault();
                    //Plane plane = axesPlaneControl1.SelectPlaneDefault();
                    Vector3 point = axesPlaneControl1.SelectPointDefault();
                    //if (iShape != null && plane != null)
                    if (iShape != null)
                    {
                        //AxesPlaneProxy axesPlaneProxy = new AxesPlaneProxy(plane, iShape);
                        AxesPlaneProxy axesPlaneProxy = new AxesPlaneProxy(point, iShape);
                        axesPlaneProxy.FindIntersections();
                        AssetManager.Instance.Add(axesPlaneProxy);
                    }
                    else
                      {
                        axesPlaneControl1.Enabled = false;
                    }
                }
                else if (name == "SlicePlane")
                {
                    regionOfInterestControl1.Visibility(false);
                    boundaryControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);
                    slicePlaneControl1.Visibility(true);
                   
                    SharedBox sBox = slicePlaneControl1.SelectBoxDefault();
                    Plane plane = slicePlaneControl1.SelectPlaneDefault();
                    if (sBox != null && plane != null)
                    {
                        //AxesPlaneProxy axesPlaneProxy = new AxesPlaneProxy(plane, iShape);
                        SlicePlaneProxy slicePlaneProxy = new SlicePlaneProxy(plane, sBox);
                        //slicePlaneProxy.FindIntersections();
                        IAssetManaged asset = AssetManager.Instance.Add(slicePlaneProxy);
                    }
                    else
                    {
                        slicePlaneControl1.Enabled = false;
                    }
                }

                else if (name == "ClippedDrawable")
                {
                    regionOfInterestControl1.Visibility(false);
                    boundaryControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    slicePlaneControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(true);
                    //IAssetManaged asset = assetManagerControl2.GetSelected()[0];
                    IDrawable iDraw = clippedDrawableControl1.SelectDrawableDefault();
                    List<ClipPlaneProxy> clipPlanes = new List<ClipPlaneProxy>();
                    ClipPlaneProxy cpp1 = clippedDrawableControl1.SelectClipPlane1Default();
                    clipPlanes.Add(cpp1);
                    ClipPlaneProxy cpp2 = clippedDrawableControl1.SelectClipPlane2Default();
                    clipPlanes.Add(cpp2);
                    ClipPlaneProxy cpp3 = clippedDrawableControl1.SelectClipPlane3Default();
                    clipPlanes.Add(cpp3);
                    if (iDraw != null && clipPlanes != null)
                    {
                        ClippedDrawableProxy cdp = new ClippedDrawableProxy(iDraw, clipPlanes);
                        IAssetManaged asset = AssetManager.Instance.Add(cdp);
                    }
                    else
                    {
                        clippedDrawableControl1.Enabled = false;
                    }
                    
                }
            }
        }

        //
        // Event handler for selection changed event
        //  This changes the ROI that the ROI control currently controls
        //
        
        
        public void assetManagerControl1_SelectedEvent(IList<IAssetManaged> items)
        {
            if (items.Count > 0)
            {
                if (items[0] is SharedBox)
                {           
                   planeControl1.Visibility(false);
                   boxControl1.Visibility(true);
                   boxControl1.SetBox((SharedBox)items[0]);
                }
                else if (items[0] is Plane)
                {
                   boxControl1.Visibility(false);
                   planeControl1.Visibility(true);
                   planeControl1.SetPlane((Plane)items[0]);
                }
            }
        }

        public void assetManagerControl2_SelectedEvent(IList<IAssetManaged> items)
        {
            if (items.Count>0)
            {
                if (items[0] is ROIBoxProxy)
                {
                    slicePlaneControl1.Visibility(false);
                    boundaryControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(true);
                    regionOfInterestControl1.SetROI(items[0] as ROIBoxProxy);

                }
                else if (items[0] is BoxBoundaryProxy)
                {
                    slicePlaneControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);
                    boundaryControl1.Visibility(true);
                    boundaryControl1.SetBoxBoundary(items[0] as BoxBoundaryProxy);                  
                }
                else if (items[0] is BoxOutlineProxy)
                {
                    slicePlaneControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(false);
                    boundaryControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);
                    outlineControl1.Visibility(true);
                    outlineControl1.SetBoxOutline(items[0] as BoxOutlineProxy);
                }
                else if (items[0] is AxesPlaneProxy)
                {
                    slicePlaneControl1.Visibility(false);
                    boundaryControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);
                    axesPlaneControl1.Visibility(true);
                    axesPlaneControl1.SetAxesPlane(items[0] as AxesPlaneProxy);
                }
                else if (items[0] is SlicePlaneProxy)
                {
                    boundaryControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);
                    slicePlaneControl1.Visibility(true);
                    slicePlaneControl1.SetSlicePlane(items[0] as SlicePlaneProxy);
                }
                else if (items[0] is ClippedDrawableProxy)
                {
                    slicePlaneControl1.Visibility(false);
                    boundaryControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(true);
                    clippedDrawableControl1.SetClippedDrawProxy(items[0] as ClippedDrawableProxy);

                }
                else if (items[0] is LayerProxy)
                {
                    boundaryControl1.Visibility(false);
                    outlineControl1.Visibility(false);
                    regionOfInterestControl1.Visibility(false);
                    axesPlaneControl1.Visibility(false);
                    slicePlaneControl1.Visibility(false);
                    clippedDrawableControl1.Visibility(false);

                }
            }
        }

        public void assetManagerControl3_SelectedEvent(IList<IAssetManaged> items)
        {
            if (items.Count > 0)
            {
                if (items[0] is Plane)
                {
                 // Do nothing .. 
                }
            }
        }


        //
        // Event handler for item updated event.  This is fire an event to tell
        //  the opengl panel to be invalidated.
        //
        public void assetManagerControl1_UpdatedEvent(IAssetManaged asset)
        {
            _eventsHelper.Fire(ChangedEvent, asset);
        }

        public void assetManagerControl2_UpdatedEvent(IAssetManaged asset)
        {
            _eventsHelper.Fire(ChangedEvent, asset);
        }

        public void assetManagerControl3_UpdatedEvent(IAssetManaged asset)
        {
            _eventsHelper.Fire(ChangedEvent, asset);
        }
        //
        // New button - Create a new shape for the user to edit
        //
        private void newButton_Click(object sender, EventArgs e)
        {
            
            if (assetManagerControl2.GetSelected().Count > 0)
            {
                if (assetManagerControl2.GetSelected()[0] is SharedBox)
                {
                    SharedBox box = new SharedBox("New Box");
                    IAssetManaged asset = AssetManager.Instance.Add(box);
                }
                else
                {
                    Plane plane = new Plane("New Plane");
                    IAssetManaged asset = AssetManager.Instance.Add(plane);
                }
            }
            else
            {
                SharedBox box = new SharedBox("New Box");
                IAssetManaged asset = AssetManager.Instance.Add(box);
            }
        }

        //
        // Remove button - remove the currently selected roi from the asset
        //  manager. 
        //
        
        private void removeToolStripButton_Click(object sender, EventArgs e)
        {
            List<IAssetManaged> list = assetManagerControl1.GetSelected();
            foreach (IAssetManaged asset in list)
            {
                AssetManager.Instance.Remove(asset);
            }
            DisableShapeControls();
        }

        private void removeDrawableButton_Click(object sender, EventArgs e)
        {
            List<IAssetManaged> list = assetManagerControl2.GetSelected();
            foreach (IAssetManaged asset in list)
            {
                AssetManager.Instance.Remove(asset);
            }
            DisableDrawableControls();
        }


        private void DisableDrawableControls()
        {
            regionOfInterestControl1.Visibility(false);
            axesPlaneControl1.Visibility(false);
            slicePlaneControl1.Visibility(false);
            boundaryControl1.Visibility(false);
            outlineControl1.Visibility(false);
            clippedDrawableControl1.Visibility(false);
        }

        private void DisableShapeControls()
        {
            boxControl1.Visibility(false);
            planeControl1.Visibility(false);
        }
        
        private void newDrawableButton_Click(object sender, EventArgs e)
        {
            if (assetManagerControl2.GetSelected().Count > 0)
            {
                IAssetManaged asset = assetManagerControl2.GetSelected()[0];
                if (asset is ROIBoxProxy)
                {
                    ROIBoxProxy roi = new ROIBoxProxy("New ROI", ((ROIBoxProxy)asset).Box);
                    AssetManager.Instance.Add(roi);
                }
                else if (asset is BoxBoundaryProxy)
                {
                    BoxBoundaryProxy bbp = new BoxBoundaryProxy("New Boundary", ((BoxBoundaryProxy)asset).Box);
                    AssetManager.Instance.Add(bbp);
                }
                else if (asset is BoxOutlineProxy)
                {
                    BoxOutlineProxy bop = new BoxOutlineProxy("New Outline", ((BoxOutlineProxy)asset).Box);
                    AssetManager.Instance.Add(bop);
                }
                else if (asset is AxesPlaneProxy)
                {
                    Vector3 pt = new Vector3(0.5f, 0.5f, 0.5f);
                    AxesPlaneProxy slp = new AxesPlaneProxy(pt, ((AxesPlaneProxy)asset).IShape);
                    AssetManager.Instance.Add(slp);
                }
                else if (asset is SlicePlaneProxy)
                {
                    SlicePlaneProxy slp = new SlicePlaneProxy(((SlicePlaneProxy)asset).Plane, ((SlicePlaneProxy)asset).IShape as SharedBox);
                    AssetManager.Instance.Add(slp);
                }
                else if (asset is ClippedDrawableProxy)
                {
                    ClippedDrawableProxy cdp = new ClippedDrawableProxy(((ClippedDrawableProxy)asset).Drawable,((ClippedDrawableProxy)asset).ClipPlanes);
                    AssetManager.Instance.Add(cdp);
                }
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create a ClipPlaneProxy
            if (orient != null && plane != null)
            {
                bool above = true;
                if(comboBox1.Text == "Below")
                    above = false;
                ClipPlaneProxy cpp = new ClipPlaneProxy(plane, above);
                IAssetManaged asset = AssetManager.Instance.Find(typeof(ClipPlaneProxy), cpp.Name);
                if (asset == null)
                {
                    asset = AssetManager.Instance.Add(cpp);
                    button1.Enabled = false;
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            orient = comboBox1.Text;
            IAssetManaged asset = AssetManager.Instance.Find(typeof(ClipPlaneProxy), "Clip" + orient + plane.Name);
            if (asset != null)
                button1.Enabled = false;
            else
                button1.Enabled = true; 
        }
    }
}
